<?php

class RM_Module_EnhancedCalendar extends RM_Module implements RM_Unit_Details_Tray_Interface, RM_Unit_List_Tray_Interface {

    public function __construct() {
        $this->name = 'EnhancedCalendar';
    }

    public function getNode() {
        return null;
    }

    protected function _getHTML(RM_Unit_Row $unit, $iconWidth = 0, $iconHeight = 0) {

        $iconHTML = "<img src='" . RM_Environment::getConnector()->getRootURL() . "RM/userdata/modules/EnhancedCalendar/images/icon.png' border='0' ";
        if ($iconWidth !== 0) {
            $iconHTML.= "width='" . $iconWidth . "px'";
        }
        if ($iconHeight !== 0) {
            $iconHTML.= "height='" . $iconHeight . "px'";
        }
        $iconHTML.= " >";

        $url = RM_Environment::getInstance()->getRouter()->_('EnhancedCalendar', 'show', array("unit_id" => $unit->id, "page" => "calendar.phtml"));

        return
        '<a onclick="RM_doShadowBox(\'' . $url . '\', \'self\', \'700\',\'500\', \'iframe\')">
            ' . $iconHTML . '
        </a>';
    }

    /**
     * returns the details html for the enhanced cal icon
     */
    public function getHTML(RM_Unit_Row $unit) {
        $model = new RM_EnhancedCalendarConfig();
        $settings = $model->getSettings()->toArray(); $settings=$settings[0];

        if ((int)$settings["show_details"]){
            return $this->_getHTML($unit);
        }
    }

    /**
     * returns the list html for the enhance cal
     */
    public function getListHTML(RM_Unit_Row $unit) {
        $model = new RM_EnhancedCalendarConfig();
        $settings = $model->getSettings()->toArray(); $settings=$settings[0];

        if ((int)$settings["show_list"]){
            return $this->_getHTML($unit, 30, 30);
        }
    }

    /**
     * Module Upgrade routine
     */
    public function upgrade(){
        // userdata
        $moduleViewFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'modules',
            $this->name,
            'views',
            'user',
            'scripts',
            'Unit'
        ));


        $mainFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit'
        ));

        // copy enhancedcal_cartbookbut_details.phtml
        $fileSource1 = implode(DIRECTORY_SEPARATOR, array($moduleViewFolderPath.DIRECTORY_SEPARATOR."details",'enhancedcal_cartbookbut_details.phtml'));
        $fileDestin1 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR."details",'enhancedcal_cartbookbut_details.phtml'));
        $renameStatus1 = rename($fileSource1, $fileDestin1);

        // copy enhancedcal_cartbookbut_details.phtml
        $fileSource2 = implode(DIRECTORY_SEPARATOR, array($moduleViewFolderPath.DIRECTORY_SEPARATOR."details",'enhancedcal_details.phtml'));
        $fileDestin2 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR."details",'enhancedcal_details.phtml'));
        $renameStatus2 = rename($fileSource2, $fileDestin2);
    }

    // installation

    public function install(){
        parent::install();

        // userdata
        $moduleViewFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'modules',
            $this->name,
            'views',
            'user',
            'scripts',
            'Unit'
        ));


        $mainFolderPath = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit'
        ));

        // copy enhancedcal_cartbookbut_details.phtml
        $fileSource1 = implode(DIRECTORY_SEPARATOR, array($moduleViewFolderPath.DIRECTORY_SEPARATOR."details",'enhancedcal_cartbookbut_details.phtml'));
        $fileDestin1 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR."details",'enhancedcal_cartbookbut_details.phtml'));
        $renameStatus1 = rename($fileSource1, $fileDestin1);

        // copy enhancedcal_cartbookbut_details.phtml
        $fileSource2 = implode(DIRECTORY_SEPARATOR, array($moduleViewFolderPath.DIRECTORY_SEPARATOR."details",'enhancedcal_details.phtml'));
        $fileDestin2 = implode(DIRECTORY_SEPARATOR, array($mainFolderPath.DIRECTORY_SEPARATOR."details",'enhancedcal_details.phtml'));
        $renameStatus2 = rename($fileSource2, $fileDestin2);

        RM_Filesystem::deleteFolder($moduleViewFolderPath);

        if ($renameStatus1 && $renameStatus2) return true;
        return false;
    }

    // uninstallation
    public function uninstall() {
        parent::uninstall();

        $file1 = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details',
            'enhancedcal_cartbookbut_details.phtml'
        ));
        RM_Filesystem::deleteFile($file1);

        $file2 = implode(DIRECTORY_SEPARATOR, array(
            RM_Environment::getConnector()->getRootPath(),
            'RM',
            'userdata',
            'views',
            'user',
            'scripts',
            'Unit',
            'details',
            'enhancedcal_details.phtml'
        ));
        RM_Filesystem::deleteFile($file2);
    }
}